package com.pbl6.back.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.pbl6.back.domain.Arrange;
import com.pbl6.back.mapper.RecordMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import xin.altitude.cms.common.entity.AjaxResult;
import java.util.List;
import com.pbl6.back.service.IRecordService;
import com.pbl6.back.domain.Record;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import xin.altitude.cms.common.entity.PageEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
@RequestMapping("/back/record")
public class RecordController{
    @Autowired
    private IRecordService recordService;
    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity,Record record){
        return AjaxResult.success(recordService.page(pageEntity.toPage(), Wrappers.lambdaQuery(record)));
    }
    @GetMapping("/list")
    public AjaxResult list(Record record){
        /*
        wrapper.lambda().like(Objects.nonNull(roleId), SysRoleResource::getRoleId, roleId);
        //----对应SQL：role_id like ‘%1%’
        wrapper.lambda().likeLeft(Objects.nonNull(roleId), SysRoleResource::getRoleId, roleId);
        //----对应SQL：role_id like ‘%1’
        wrapper.lambda().likeRight(Objects.nonNull(roleId), SysRoleResource::getRoleId, roleId);
        //----对应SQL：role_id like ‘1%’
        */
        LambdaQueryWrapper<Record> recordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (record.getOperator() !=null&&" "!=record.getOperator()) {
            recordLambdaQueryWrapper.like(Record::getOperator,record.getOperator());
        }
        if (record.getRubric() !=null&&" "!=record.getRubric()) {
        recordLambdaQueryWrapper.like(Record::getRubric,record.getRubric());
        }
        if (record.getJournal() !=null&&" "!=record.getJournal()) {
            recordLambdaQueryWrapper.like(Record::getJournal, record.getJournal());
        }
        return AjaxResult.success(recordService.list(recordLambdaQueryWrapper));
    }
    @PostMapping("/push")
    public AjaxResult add(@RequestBody Record record) {
        return AjaxResult.success(recordService.save(record));
    }
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Record record) {
        return AjaxResult.success(recordService.updateById(record));
    }
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(recordService.removeByIds(Arrays.asList(ids)));
    }
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(recordService.getById(id));
    }
}
